﻿/*********************************************************************************
 * Copyright(c)  	2012 @kaleyroy
 * 创建日期：		2012-01-12 23:10:44
 * 文 件 名：		LevelRepository.cs 
 * CLR 版本: 		4.0.30319.239 
 * 创 建 者：		@kaleyroy 
 * 电子邮箱:		kaleyroy@gmail.com
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using USS.Domain;

/// <summary>
/// 
/// 修改记录:
///	
/// </summary>
namespace USS.Repository
{
    /// <summary>
    /// 级别数据访问对象
    /// </summary>
    public class LevelRepository : USSRepository<Level>
    {
        public override void Add(Level entity)
        {
            ussCxt.Level.InsertOnSubmit(entity);
            ussCxt.SubmitChanges();
        }

        public override bool Exists(LinqSpecs.Specification<Level> specification)
        {
            var query = ussCxt.Level.Where(specification.IsSatisfiedBy());
            return query.Count() > 0;
        }

        public override Level Find(LinqSpecs.Specification<Level> specification)
        {
            var query = ussCxt.Level.Where(specification.IsSatisfiedBy());
            if (query.Count() > 0)
                return query.First();
            return null;
        }

        public override IEnumerable<Level> FindAll()
        {
            return ussCxt.Level;
        }

        public override IEnumerable<Level> FindAll(LinqSpecs.Specification<Level> specification)
        {
            return ussCxt.Level.Where(specification.IsSatisfiedBy());
        }

        public override Level GetByKey(object key)
        {
            int levelID = (int)key;
            return ussCxt.Level.SingleOrDefault(l => l.LevelID == levelID);
        }

        public override void Remove(Level entity)
        {
            ussCxt.Level.DeleteOnSubmit(entity);
            ussCxt.SubmitChanges();
        }

        public override void Update(Level entity)
        {
            Level level = GetByKey(entity.LevelID);
            level.LevelName = entity.LevelName;
            level.BillCount = entity.BillCount;
            level.LevelColor = entity.LevelColor;
            level.IsEnabled = entity.IsEnabled;
            ussCxt.SubmitChanges();
        }
    }
}
